package com.icity.controller.visit;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.icity.common.Result;
import com.icity.entity.EchartLine;
import com.icity.entity.visit.VisitAll;
import com.icity.service.VisitService;
import com.icity.util.DateUtils;
//广告统计
@RestController
@RequestMapping(value = "/admin/visitstatistical")
public class VisitController {
	@Autowired
	private VisitService visitService;

	@RequestMapping(value = "/surveyline")
	@ResponseBody
	public Result getArticelLine(HttpServletRequest request) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Map<Object,Object> params = new HashMap<Object,Object>();
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		int days = Integer.parseInt(request.getParameter("days"));
		System.out.println(days);
		if (days == 66) {
			params.put("startDate", startDate);
			params.put("endDate", endDate);
		}else{
			params.put("nowDate", sdf.format(new Date()));
			params.put("startDate", DateUtils.getSpecifiedDayBefore(sdf.format(new Date()), days));
			params.put("endDate", sdf.format(new Date()));
		}
		Result result = new Result();
		try {
			EchartLine echartLine = new EchartLine();
			Map<Object,String> tooltip = visitService.getTooltip();
			echartLine.setTooltip(tooltip);

			Map<Object,List<String>> legend = visitService.getLegend();
			echartLine.setLegend(legend);

			Map<Object,Object> grid = visitService.getGrid();
			echartLine.setGrid(grid);

			Map<Object,Object> xAxis = visitService.getWeekDay(params, days);
			echartLine.setxAxis(xAxis);

			Map<Object,Object> yAxis = visitService.getYaxis();
			echartLine.setyAxis(yAxis);

			List<Map<Object,Object>> listSeries = visitService.getVisitSeries(params);
			echartLine.setSeries(listSeries);

			result.setData(echartLine);
			result.setCode("1");
			result.setMessage("获取数据成功");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode("-1");
			result.setMessage("获取数据异常");
			return result;
		}
	}

	@RequestMapping(value = "/allnum")
	@ResponseBody
	public Result getAllVisit(){
		Result result = new Result();
		try {
			VisitAll visitAll = visitService.getAllVisit();
			result.setData(visitAll);
			result.setCode("1");
			result.setMessage("获取数据成功");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode("-1");
			result.setMessage("获取数据异常");
			return result;
		}
	}
}
